#include "geometry.h"
#include <cmath>

namespace lodestar
{
    Vector3 Vector3::normalize()
    {
        float len = length();

        //if (len == 0.0f)
        
        return Vector3(x / len, y / len, z / len);
    }

    Vector3 Vector3::operator - ( const Vector3& other )
    {
        return Vector3(x - other.x, y - other.y, z - other.z);
    }

    Vector3 Vector3::operator*( const float scalar )
    {
        return Vector3(x * scalar, y * scalar, z * scalar);
    }

    const Vector3& Vector3::operator*=( const float scalar )
    {
        x *= scalar;
        y *= scalar;
        z *= scalar;

        return *this;
    }

    const Vector3& Vector3::operator+=( const Vector3& other )
    {
        x += other.x;
        y += other.y;
        z += other.z;

        return *this;
    }

    float Vector3::length() const
    {
        return sqrt(x * x + y * y + z * z);
    }

    Vector3 Vector3::truncate( float l )
    {
        if (length() <= l )
            return *this;

        Vector3 result = normalize();
        result.x *= l;
        result.y *= l;
        result.z *= l;

        return result;
    }

}
